package com.cocoiko.tools.facebook;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;

import com.cocoiko.tools.facebook.SessionEvents.AuthListener;
import com.cocoiko.tools.facebook.SessionEvents.LogoutListener;
import com.cocoiko.tools.twitter.Constants;
import com.cocoiko.utils.CocoikoConstains;
import com.facebook.android.AsyncFacebookRunner;
import com.facebook.android.DialogError;
import com.facebook.android.Facebook;
import com.facebook.android.FacebookError;
import com.facebook.android.Facebook.DialogListener;

public class FacebookLogin {

	static String PERMISSTION[] = {};

	Activity parent;
	Facebook facebook;
	AsyncFacebookRunner facebookRunner;
	Handler handler = new Handler();

	public FacebookLogin(Activity parent) {
		// TODO Auto-generated constructor stub
		this.parent = parent;
		facebook = new Facebook(CocoikoConstains.FACEBOOK_ID);
		facebookRunner = new AsyncFacebookRunner(facebook);

		SessionStore.restore(facebook, parent);
		SessionEvents.addAuthListener(new FacebookOAuthListener());
		SessionEvents.addLogoutListener(new FacebookLogoutListener());

		// Auth
		SessionEvents.addAuthListener(new SessionListener());
		SessionEvents.addLogoutListener(new SessionListener());
		

	}
	
	public boolean isOAuth(){
		return facebook.isSessionValid();
	}

	public void facebookOAuth() {
		if (facebook.isSessionValid()) {
			// SessionEvents.onLogoutBegin();
			// facebookRunner = new AsyncFacebookRunner(facebook);
			// facebookRunner.logout(parent.getApplicationContext(),
			// new LogoutRequestListener());
			Log.i("Facebook is session valid", "true");
		} else {
			facebook.authorize(parent, PERMISSTION, new LoginDialogListener());
		}
	}

	private class FacebookOAuthListener implements AuthListener {

		@Override
		public void onAuthFail(String error) {
			// TODO Auto-generated method stub

		}

		@Override
		public void onAuthSucceed() {
			// TODO Auto-generated method stub

		}

	}

	private class FacebookLogoutListener implements LogoutListener {

		@Override
		public void onLogoutBegin() {
			// TODO Auto-generated method stub

		}

		@Override
		public void onLogoutFinish() {
			// TODO Auto-generated method stub

		}

	}

	private class SessionListener implements AuthListener, LogoutListener {

		@Override
		public void onAuthFail(String error) {
			// TODO Auto-generated method stub

		}

		@Override
		public void onAuthSucceed() {
			// TODO Auto-generated method stub
			SessionStore.save(facebook, parent);
		}

		@Override
		public void onLogoutBegin() {
			// TODO Auto-generated method stub

		}

		@Override
		public void onLogoutFinish() {
			// TODO Auto-generated method stub
			SessionStore.clear(parent);
		}

	}

	private class LogoutRequestListener extends BaseRequestListener {

		@Override
		public void onComplete(String response, Object state) {
			// TODO Auto-generated method stub
			handler.post(new Runnable() {

				@Override
				public void run() {
					// TODO Auto-generated method stub
					SessionEvents.onLogoutFinish();
				}
			});
		}

	}

	private final class LoginDialogListener implements DialogListener {
		public void onComplete(Bundle values) {
			SessionEvents.onLoginSuccess();
		}

		public void onFacebookError(FacebookError error) {
			SessionEvents.onLoginError(error.getMessage());
		}

		public void onError(DialogError error) {
			SessionEvents.onLoginError(error.getMessage());
		}

		public void onCancel() {
			SessionEvents.onLoginError("Action Canceled");
		}
	}
}
